<template>
  <div>
    <el-form>
      <el-button-group style="margin-bottom:20px;">
        <el-button size="small" icon="el-icon-view" @click="link_detail">查看</el-button>
        <el-button size="small" icon="el-icon-edit">编辑</el-button>
        <el-button size="small" icon="el-icon-circle-close-outline">删除</el-button>
        <el-button size="small" icon="el-icon-plus">新增</el-button>
        <el-button size="small" icon="el-icon-plus" :disabled="multipleSelection.length === 0" @click="handleDownloadXlsx(multipleSelection)">下载</el-button>
      </el-button-group>
    </el-form>
    <el-table
      :data="currentTableData"
      v-loading="loading"
      size="mini"
      stripe
      style="width: 100%;"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column label="编号" width="50" align="center">
        <template slot-scope="scope">
          {{scope.row.key}}
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="100" align="center">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="性别" width="50" align="center">
        <template slot-scope="scope">
          {{scope.row.sex}}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" width="200" align="center">
        <template slot-scope="scope">
          {{scope.row.IdCard}}
        </template>
      </el-table-column>
      <el-table-column label="出生年月" width="100" align="center">
        <template slot-scope="scope">
          {{scope.row.birthday}}
        </template>
      </el-table-column>
      <el-table-column label="年龄" width="80" align="center">
        <template slot-scope="scope">
          {{scope.row.age}}
        </template>
      </el-table-column>
      <el-table-column label="手机号码" width="100" align="center">
        <template slot-scope="scope">
          {{scope.row.tel}}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag
            size="mini"
            type="info" v-if="scope.row.state==0">
            正常
          </el-tag>
          <el-tag
            size="mini"
            type="warning" v-if="scope.row.state==1">
            非正常
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import BooleanControl from '../BooleanControl'
import BooleanControlMini from '../BooleanControlMini'
export default {
  components: {
    BooleanControl,
    BooleanControlMini
  },
  props: {
    tableData: {
      default: () => []
    },
    loading: {
      default: false
    }
  },
  data () {
    return {
      currentTableData: [],
      multipleSelection: [],
      downloadColumns: [
        { label: '编号', prop: 'key' },
        { label: '名称', prop: 'name' },
        { label: '性别', prop: 'sex' },
        { label: '身份证号', prop: 'IdCard' },
        { label: '出生日期', prop: 'birthday' },
        { label: '年龄', prop: 'age' },
        { label: '联系方式', prop: 'tel' },
        { label: '状态', prop: 'state' }
      ]
    }
  },
  watch: {
    tableData: {
      handler (val) {
        this.currentTableData = val
      },
      immediate: true
    }
  },
  methods: {
    link_detail () {
      var id = this.multipleSelection[0].id
      console.log(id)
      this.$router.push(`/customer/infolist/detail?id=${id}`)
    },
    handleSwitchChange (val, index) {
      const oldValue = this.currentTableData[index]
      this.$set(this.currentTableData, index, {
        ...oldValue,
        type: val
      })
      // 注意 这里并没有把修改后的数据传递出去 如果需要的话请自行修改
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    downloadDataTranslate (data) {
      return data.map(row => ({
        ...row,
        used: row.used ? '离职' : '在职'
      }))
    },
    handleDownloadXlsx (data) {
      this.$export.excel({
        title: 'D2Admin 表格示例',
        columns: this.data,
        data: this.downloadDataTranslate(data)
      })
        .then(() => {
          this.$message('导出表格成功')
        })
    }
  }
}
</script>
